package com.zhs.gccs.activity.gccs;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.shape.view.ShapeButton;
import com.zhs.gccs.R;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.http.Api;
import com.zhs.gccs.http.ProgressObserver;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.ToastUtils;
import com.zhs.gccs.vo.AdminPeopleBean;
import com.zhs.gccs.vo.AdminTaskBean;
import com.zhs.gccs.vo.BaseBean;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.RequestBody;

/**
 * Created by chengzf on 2024/7/19.
 */
public class AdminSendTaskConfirmActivity extends BaseActivity {
    @BindView(R.id.fame_bar)
    FrameLayout fame_bar;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.btn_back)
    ShapeButton btn_back;
    @BindView(R.id.btn_next)
    ShapeButton btn_next;
    @BindView(R.id.tv_task_name)
    TextView tv_task_name;
    @BindView(R.id.tv_task_people)
    TextView tv_task_people;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_task_info_layout);
        ImmersionBar.with(this).transparentBar().statusBarDarkFont(false).init();
        ButterKnife.bind(this);
        fame_bar.setPadding(0, ImmersionBar.getStatusBarHeight(this), 0, 0);

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        AdminTaskBean taskBean = (AdminTaskBean) getIntent().getSerializableExtra("task");
        AdminPeopleBean peopleBean = (AdminPeopleBean) getIntent().getSerializableExtra("people");
        tv_task_name.setText(taskBean.getXmmc());
        tv_task_people.setText(peopleBean.getNickName());
    }

    private void initView() {
        iv_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });

        btn_back.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });

        btn_next.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                AdminTaskBean taskBean = (AdminTaskBean) getIntent().getSerializableExtra("task");
                AdminPeopleBean peopleBean = (AdminPeopleBean) getIntent().getSerializableExtra("people");

                Map hashMap = new HashMap<>();
                hashMap.put("pid", taskBean.getPid());
                hashMap.put("userExec", peopleBean.getUserName());
                RequestBody requestBody = createRequestBody(hashMap);
                toSubscribeSingleOb(Api.getDefault().saveGccsTask(requestBody),
                        new ProgressObserver(AdminSendTaskConfirmActivity.this, false) {
                            @Override
                            protected void _onNext(String result) {
                                BaseBean bean = new Gson().fromJson(result, BaseBean.class);
                                if (bean != null && bean.getCode() == 200) {
                                    ToastUtils.toast("提交成功");
                                    AdminGccsTaskActivity.start(AdminSendTaskConfirmActivity.this);
                                } else {
                                    ToastUtils.toast("提交失败");
                                }
                            }

                            @Override
                            protected void _onError(String result) {
                                ToastUtils.toast("提交失败");
                            }
                        });
            }
        });
    }

    public static void start(Context context, AdminTaskBean taskBean, AdminPeopleBean peopleBean) {
        Intent intent = new Intent(context, AdminSendTaskConfirmActivity.class);
        intent.putExtra("task", taskBean);
        intent.putExtra("people", peopleBean);
        context.startActivity(intent);
    }
}
